package com.weai.portal.controller;

import com.weai.portal.entity.Solution;
import com.weai.portal.service.SolutionService;
import com.weai.portal.common.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/solutions")
@RequiredArgsConstructor
@Tag(name = "标准方案库", description = "管理AI解决方案的创建、查询、查看和编辑")
@Slf4j
public class SolutionController {

    private final SolutionService solutionService;

    @GetMapping
    @Operation(summary = "获取方案列表", description = "获取所有方案或根据关键词搜索")
    public ApiResponse<List<Solution>> getAllSolutions(@RequestParam(required = false) String keyword) {
        log.info("开始获取解决方案列表，搜索关键词: {}", keyword);
        try {
            List<Solution> solutions;
            if (keyword != null && !keyword.isEmpty()) {
                log.debug("使用关键词搜索解决方案: {}", keyword);
                solutions = solutionService.searchSolutions(keyword);
            } else {
                log.debug("获取所有解决方案");
                solutions = solutionService.getAllSolutions();
            }
            log.info("成功获取解决方案列表，数量: {}", solutions.size());

            log.debug("解决方案列表: {}", ApiResponse.success(solutions));
            return ApiResponse.success(solutions);
        } catch (Exception e) {
            log.error("获取解决方案列表失败", e);
            return ApiResponse.error(500, "获取解决方案列表失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取方案详情", description = "根据ID获取方案详情")
    public ApiResponse<Solution> getSolutionById(@PathVariable Integer id) {
        log.info("开始获取解决方案详情，ID: {}", id);
        try {
            Solution solution = solutionService.getSolutionById(id);
            log.info("成功获取解决方案详情，ID: {}", id);
            return ApiResponse.success(solution);
        } catch (Exception e) {
            log.error("获取解决方案详情失败，ID: {}", id, e);
            return ApiResponse.error(500, "获取解决方案详情失败");
        }
    }

    @PostMapping
    @Operation(summary = "创建方案", description = "创建新的解决方案")
    public ApiResponse<Solution> createSolution(@RequestBody Solution solution) {
        log.info("开始创建解决方案，方案名称: {}", solution.getSolutionName());
        try {
            Solution createdSolution = solutionService.createSolution(solution);
            log.info("成功创建解决方案，ID: {}", createdSolution.getId());
            return ApiResponse.success(createdSolution);
        } catch (Exception e) {
            log.error("创建解决方案失败，方案名称: {}", solution.getSolutionName(), e);
            return ApiResponse.error(500, "创建解决方案失败");
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新方案", description = "更新指定ID的解决方案")
    public ApiResponse<Solution> updateSolution(@PathVariable Integer id, @RequestBody Solution solution) {
        log.info("开始更新解决方案，ID: {}", id);
        try {
            Solution updatedSolution = solutionService.updateSolution(id, solution);
            log.info("成功更新解决方案，ID: {}", id);
            return ApiResponse.success(updatedSolution);
        } catch (Exception e) {
            log.error("更新解决方案失败，ID: {}", id, e);
            return ApiResponse.error(500, "更新解决方案失败");
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除方案", description = "删除指定ID的解决方案")
    public ApiResponse<Void> deleteSolution(@PathVariable Integer id) {
        log.info("开始删除解决方案，ID: {}", id);
        try {
            solutionService.deleteSolution(id);
            log.info("成功删除解决方案，ID: {}", id);
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("删除解决方案失败，ID: {}", id, e);
            return ApiResponse.error(500, "删除解决方案失败");
        }
    }
} 